package cn.yinuo.website.common.util;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wu
 * @date 2018/07/25
 * @Description:
 */
public class ListUtil {
    public static List<Long> StringListToLong(List<String> stringList) {
        List<Long> longList = new ArrayList<>();
        if (ArrayUtils.isEmpty(stringList)) {
            return longList;
        }
        try {
            for (String s : stringList) {
                if (StringUtils.isBlank(s) || !StringUtils.isNumeric(s)) {
                    continue;
                }
                longList.add(Long.parseLong(s));
            }
        } catch (Exception e) {
            return longList;
        }
        return longList;
    }

    public static List<Integer> StringListToInteger(List<String> stringList) {
        List<Integer> longList = new ArrayList<>();
        if (ArrayUtils.isEmpty(stringList)) {
            return longList;
        }
        try {
            for (String s : stringList) {
                if (StringUtils.isBlank(s) || !StringUtils.isNumeric(s)) {
                    continue;
                }
                longList.add(Integer.parseInt(s));
            }
        } catch (Exception e) {
            return longList;
        }
        return longList;
    }

    public static List<String> LongListToString(List<Long> longList) {
        List<String> stringList = new ArrayList<>();
        if (ArrayUtils.isEmpty(longList)) {
            return stringList;
        }
        try {
            for (Long i : longList) {
                stringList.add(String.valueOf(i));
            }
        } catch (Exception e) {
            return stringList;
        }
        return stringList;
    }

    public static List<Long> StringArrayToLongList(String[] strArray) {
        List<Long> longList = new ArrayList<>();
        try {
            for (int i = 0; i < strArray.length; i++) {
                longList.add(Long.parseLong(strArray[i]));
            }
        } catch (Exception e) {
            return longList;
        }
        return longList;
    }

    public static List<Integer> StringArrayToIntegerList(String[] strArray) {
        List<Integer> intList = new ArrayList<>();
        try {
            for (int i = 0; i < strArray.length; i++) {
                intList.add(Integer.parseInt(strArray[i]));
            }
        } catch (Exception e) {
            return intList;
        }
        return intList;
    }

    public static <T> void addAll(List<T> allList, List<T> addList) {
        if (!ArrayUtils.isEmpty(addList)) {
            allList.addAll(addList);
        }
    }

    public static List<String> commaStringToListString(String s) {
        List<String> list = new ArrayList<>();
        if (StringUtils.isBlank(s)) {
            return list;
        }
        String[] a = s.split(",");
        for (int i = 0; i < a.length; i++) {
            list.add(a[i]);
        }
        return list;
    }

    public static List<String> commaStringToListString(String s, String prefix) {
        List<String> list = new ArrayList<>();
        if (StringUtils.isBlank(s)) {
            return list;
        }
        String[] a = s.split(",");
        for (int i = 0; i < a.length; i++) {
            list.add(prefix + a[i]);
        }
        return list;
    }

    public static String ListStringToCommaString(List<String> stringList) {
        String s = "";
        if (ArrayUtils.isEmpty(stringList)) {
            return s;
        }
        for (String str : stringList) {
            s += str + ",";
        }
        if (!StringUtils.isBlank(s)) {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }

    public static <T, V, E> void fillParentsChildrenList(List<T> parents, List<E> children,
                                                         List<V> relations, String primaryKey, String relationParentKey,
                                                         String getRelationChildId, String setChildrenList) {
        Map<String, T> parentMap = ArrayUtils.list2Map(parents, primaryKey);
        Map<String, E> childMap = ArrayUtils.list2Map(children, primaryKey);
        Map<String, List<V>> parentChildRelationMap = ArrayUtils.list2MapList(relations, relationParentKey);
        try {
            for (String key : parentMap.keySet()) {
                T parent = parentMap.get(key);
                if (parent == null) {
                    continue;
                }
                List<V> parentChildRelations = parentChildRelationMap.get(key);
                if (ArrayUtils.isEmpty(parentChildRelations)) {
                    continue;
                }
                List<E> childList = new ArrayList<>();
                Method m = parent.getClass().getMethod(setChildrenList, List.class);
                for (V parentChildRelation : parentChildRelations) {
                    Method method = parentChildRelation.getClass().getMethod(getRelationChildId);
                    E Child = childMap.get(method.invoke(parentChildRelation));
                    if (Child == null) {
                        continue;
                    }
                    childList.add(Child);
                }
                m.invoke(parent, childList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static <T, V> void fillParentsChildrenList(List<T> parents, List<V> children, String primaryKey,
                                                      String childParentKey, String setChildrenList) {
        Map<String, T> parentMap = ArrayUtils.list2Map(parents, primaryKey);
        Map<String, List<V>> parentChildrenMap = ArrayUtils.list2MapList(children, childParentKey);
        try {
            for (String key : parentMap.keySet()) {
                T parent = parentMap.get(key);
                if (parent == null) {
                    continue;
                }
                List<V> childrenList = parentChildrenMap.get(key);
                Method method = parent.getClass().getMethod(setChildrenList, List.class);
                if (ArrayUtils.isEmpty(childrenList)) {
                    continue;
                }
                method.invoke(parent, childrenList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<String> deReplicateList(List<String> stringList) {
        if (ArrayUtils.isEmpty(stringList)) {
            return stringList;
        }
        List<String> retList = new ArrayList<>();
        for (String s : stringList) {
            if (StringUtils.isBlank(s) || retList.contains(s)) {
                continue;
            }
            retList.add(s);
        }
        return retList;
    }

    public static List<Long> reOrderList(List<Long> list, List<Long> orderList) {
        List<Long> retList = new ArrayList<>();
        if (ArrayUtils.isEmpty(list) || ArrayUtils.isEmpty(orderList)) {
            return retList;
        }
        for (Long id : orderList) {
            if (list.contains(id)) {
                retList.add(id);
            }
        }
        return retList;
    }

    public static List<String> trim(List<String> list) {
        if (ArrayUtils.isEmpty(list)) {
            return list;
        }
        List<String> retList = new ArrayList<>();
        for (String s : list) {
            retList.add(s.trim());
        }
        return retList;
    }
}
